//
//  LWShopCarViewController.m
//  LWShopItem
//
//  Created by mac on 17/3/14.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWShopCarViewController.h"
#import "UIColor+Addition.h"
#import "LWHomeViewController.h"
#import <Masonry.h>
#import "LWShopCarTBottomiew.h"
#import "LWShopCarCellTableViewCell.h"
#import "LWShopCarCellSectionHeader.h"
#import "LWAccountViewController.h"
#import "LWAddressViewController.h"

static NSString *shopCarCell = @"shopCarCell";
static NSString *shopCarCellHeader = @"shopCarCellHeader";

@interface LWShopCarViewController ()<UITableViewDataSource,UITableViewDelegate,LWShopCarCellTableViewCellDelegate,LWShopCarTBottomiewDelegate>

@property(nonatomic,weak)LWShopCarTBottomiew *bottomView;
@property(nonatomic,weak)UITableView *tab;

/** 记录需要支付的货物信息 */
@property (nonatomic, strong) NSMutableArray *shopArrM;

@end

@implementation LWShopCarViewController{
    NSMutableArray *_deleteArr;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupNavgationBar];
    
    [self setupUI];
    _deleteArr = [NSMutableArray array];
    
}

#pragma mark - 导航栏的设置
- (void)setupNavgationBar {
    
    self.title = @"购物车";
   
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHex:0x9ACD32];
   
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(backCLick)];
    
    self.navigationItem.leftBarButtonItem = backBtnItem;
    
    
    
    
}
- (void)backCLick {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:LWShopCarNotification object:nil userInfo:@{@"DeleteArr":_deleteArr}];
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.selectedViewController = self.tabBarController.viewControllers[0];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [self.tab reloadData];
    
    //解决一个大bug
    self.bottomView.isSelectedAll = [self goodSelectedAllOrNotInShopCarArr];
    
    
}
#pragma mark - 主界面设置
- (void)setupUI {
    
    LWShopCarTBottomiew *bottomView = [LWShopCarTBottomiew shopCarBottomView];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(@80);
    }];
    bottomView.delegate = self;
    _bottomView = bottomView;
    
    UITableView *tab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    tab.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tab];
    [tab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(64);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    tab.showsVerticalScrollIndicator = NO;
    tab.dataSource = self;
    tab.delegate = self;
    tab.rowHeight = 110;
    [tab registerNib:[UINib nibWithNibName:@"LWShopCarCellTableViewCell" bundle:nil] forCellReuseIdentifier:shopCarCell];
    [tab registerNib:[UINib nibWithNibName:@"LWShopCarCellSectionHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:shopCarCellHeader];
    self.tab = tab;
}
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _shopCarArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LWShopCarCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: shopCarCell forIndexPath:indexPath];
    cell.selectedBackgroundView = [[UIView alloc] init];
    cell.model = _shopCarArr[indexPath.section];
    cell.delegate = self;
    return cell;
}
#pragma mark - UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    LWShopCarCellSectionHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:shopCarCellHeader];
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
#pragma maek - LWShopCarTopViewDelegate
- (void)quitFromShopCar {
    [self backCLick];
}
#pragma mark - LWShopCarCellTableViewCellDelegate
- (void)deleteFromShopCarWithModel:(LWDetailModel *)model {
    [_shopCarArr removeObject:model];
    [_deleteArr addObject:model];
    [self.tab reloadData];
    NSLog(@"%@", _deleteArr);
    self.bottomView.allPrice = [self calculateAllPayMoney];
}
- (void)changeShopCount {
    [self.tab reloadData];
    self.bottomView.allPrice = [self calculateAllPayMoney];
}
- (void)notifyShopCarChangeModelState {
    __block BOOL result = YES;
    [_shopCarArr enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.isSeleted == NO) {
            result = NO;
        }
    }];
    _bottomView.isSelectedAll = result;
    self.bottomView.allPrice = [self calculateAllPayMoney];
}
#pragma mark - LWShopCarTBottomiewDelegate
- (void)selectAllOrCancleAll:(BOOL)isSelected {
    if (isSelected) {
        [_shopCarArr enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.isSeleted = YES;
        }];
    } else {
        [_shopCarArr enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.isSeleted = NO;
        }];
    }
    [self.tab reloadData];
    
    self.bottomView.allPrice = [self calculateAllPayMoney];
}

- (void)jumpToPay {
    LWAccountViewController *vc = [[LWAccountViewController alloc] init];
    vc.accountArr = [self accountArr];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 计算应付钱数
- (double)calculateAllPayMoney {
    __block double allPrice = 0;
    [_shopCarArr enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%@", obj.goods_id);
        if (obj.isSeleted) {
            allPrice += obj.shop_price.doubleValue * obj.orderCount;
        }
    }];
    return allPrice;
}
#pragma  mark - 物品是否已经全选中
- (BOOL)goodSelectedAllOrNotInShopCarArr {
    if (_shopCarArr.count == 0) {
        return NO;
    }
    __block BOOL indicator = YES;
    [_shopCarArr enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.isSeleted == NO) {
            indicator = NO;
        }
        if (indicator == NO) {
            *stop = YES;
        }
    }];
    return indicator;;
}
#pragma  mark - 每次要跳转到支付时需要购买物品的array
- (NSArray *)accountArr {
    NSMutableArray *tempArr = [NSMutableArray array];
    [_shopCarArr enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.isSeleted == YES) {
            [tempArr addObject:obj];
        }
    }];
    return tempArr.copy;
}


@end
